package com.fhz.day014;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 时间:2022/6/1
 *
 * @author msfeng
 * 简述: TODO
 */
public class FragmentRefreshLayout extends FrameLayout {

    private static final String TAG = "FragmentRefreshLayout";

    private LinearLayout mRefreshLayout;

    private int mMaxRefreshHeight = 200;

    private PointF mBeforePointF;  //点击xy的坐标

    //new
    public FragmentRefreshLayout(@NonNull Context context) {
        this(context,null);
    }

    //xml
    public FragmentRefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
       this(context,attrs,0);
    }

    //
    public FragmentRefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public FragmentRefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        Log.i(TAG, "init: ");

        mBeforePointF = new PointF();

        mRefreshLayout = new LinearLayout(getContext());
        LayoutParams refreshParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, mMaxRefreshHeight);
        refreshParams.topMargin = - mMaxRefreshHeight;
        mRefreshLayout.setLayoutParams(refreshParams);
        mRefreshLayout.setBackgroundColor(Color.RED);

        addView(mRefreshLayout);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            mBeforePointF.x = ev.getX();
            mBeforePointF.y = ev.getY();
        } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            //偏移量
            float xDimension = Math.abs(ev.getX() - mBeforePointF.x);
            float yDimension = Math.abs(ev.getY() - mBeforePointF.y);
            //判读滑动方向
            if (xDimension > yDimension * 2) {
                //X轴滑动
                return super.dispatchTouchEvent(ev);
            }
            //最小响应距离
            if (yDimension < 5) {
                return super.dispatchTouchEvent(ev);
            }
            //可滑动范围
            //Main下拉
            FrameLayout.LayoutParams mainParams = (LayoutParams) mRefreshLayout.getLayoutParams();
            mainParams.topMargin += ev.getY() - mBeforePointF.y;

            if (mainParams.topMargin > 0) {
                mainParams.topMargin = 0;
            }
//
            if (mainParams.topMargin < -mMaxRefreshHeight) {
                mainParams.topMargin = -mMaxRefreshHeight;
            }

            mRefreshLayout.setLayoutParams(mainParams);


            mBeforePointF.x = ev.getX();
            mBeforePointF.y = ev.getY();
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            //当滑动位置，打开或关闭
            LayoutParams mainParams = (LayoutParams) mRefreshLayout.getLayoutParams();
            if (mainParams.topMargin < -mMaxRefreshHeight / 2 ) {
                startAnim(mainParams.topMargin, -mMaxRefreshHeight);
            } else {
                startAnim(mainParams.topMargin, 0);
            }
        }
        return true;
    }
    private void startAnim(int start, int end) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        valueAnimator.setDuration(500);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                FrameLayout.LayoutParams mainParams = (LayoutParams) mRefreshLayout.getLayoutParams();
                mainParams.topMargin = (int) animation.getAnimatedValue();
                mRefreshLayout.setLayoutParams(mainParams);
//                requestLayout();
            }
        });
        valueAnimator.start();
    }
}

